/*
 * Copyright (C) 2013 Roy Wang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dolphinwang.imagecoverflow;

import ohos.media.image.PixelMap;
import ohos.media.image.common.ImageInfo;
import ohos.media.image.common.Rect;
import ohos.media.image.common.ScaleMode;
import ohos.media.image.common.Size;

import java.util.Optional;

/**
 * Bitmap Utils
 */
public class BitmapUtils {
    /**
     * create Reflected Bitmap
     *
     * @param pixelMap      PixelMap
     * @param reflectHeight float
     * @return PixelMap
     */
    public static Optional<PixelMap> createReflectedBitmap(PixelMap pixelMap, float reflectHeight) {
        if (pixelMap == null || pixelMap.getImageInfo() == null) {
            return Optional.empty();
        }
        ImageInfo info = pixelMap.getImageInfo();
        int srcWidth = info.size.width;
        int srcHeight = info.size.height;
        int reflectionWidth = info.size.width;
        int reflectionHeight = reflectHeight == 0 ? srcHeight / 3 : (int) (reflectHeight * srcHeight);

        if (srcWidth == 0 || srcHeight == 0) {
            return Optional.empty();
        }
        Rect rect = new Rect(0, srcHeight - reflectionHeight, reflectionWidth, reflectionHeight);
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.scaleMode = ScaleMode.CENTER_CROP;
        options.size = new Size(reflectionWidth, reflectionHeight);
        return Optional.of(PixelMap.create(pixelMap, rect, options));
    }

    /**
     * get PixMap Width
     *
     * @param pixelMap PixelMap
     * @return int
     */
    public static int getPixMapWidth(PixelMap pixelMap) {
        if (pixelMap == null) {
            return 0;
        }
        return pixelMap.getImageInfo().size.width;
    }

    /**
     * get PixMap Height
     *
     * @param pixelMap PixelMap
     * @return int
     */
    public static int getPixMapHeight(PixelMap pixelMap) {
        if (pixelMap == null) {
            return 0;
        }
        return pixelMap.getImageInfo().size.height;
    }
}
